package com.culture.bootdemo.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wen
 * @date 2019/12/13 10:14
 * @Description
 **/
public class ValidUtil {

    private static final Pattern patternPhone = Pattern.compile("\\d{11}");
    private static final Pattern patternMoney = Pattern.compile("\\d{4}");


    /**
     * 校验手机号
     *
     * @param phone
     * @return
     */
    public static boolean verifyPhone(String phone) {
        String regex = "^[1]([3-9])[0-9]{9}$";
        boolean isMatch = false;
        if (StringUtils.isEmpty(phone)) {
            return isMatch;
        } else if (phone.length() != 11) {
            return isMatch;
        } else {
            isMatch = Pattern.matches(regex, phone);
        }
        return isMatch;
    }

    /**
     * 校验数字
     *
     * @param str
     * @return
     */
    public static boolean verifyNum(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        return str.matches("[0-9]*");
    }

    /**
     * 是否是手机号
     *
     * @param number 15735159811
     * @return boolean
     */
    public static boolean isCorrectPhone(String number) {
        try {

            Matcher matcher = patternPhone.matcher(number);
            return (number.length() == 11) && matcher.matches();
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        boolean correctPhone = isCorrectMoney("2332.005");
        System.out.println(correctPhone);
    }


    /**
     * 是否是金额
     *
     * @param money 1.00
     * @return boolean
     */
    private static boolean isCorrectMoney(String money) {
        String[] strs = money.split("\\.");
        if (strs.length > 2) {
            return false;
        }
        if (strs[0].length() > 9 || strs[0].length() < 1) {
            return false;
        }
        if (strs.length == 2 && (strs[1].length() > 2 || strs[1].length() < 1)) {
            return false;
        }
        try {
            BigDecimal i = new BigDecimal(money);
            return i.compareTo(new BigDecimal(0)) == 1;
        } catch (Exception e) {
            return false;
        }
    }
}
